﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class ComponentService
    {
        internal static ComponentOut ComponentResult(ComponentIn componentIn)
        {
            ComponentOut componentOut = new ComponentOut();

            //钢筋重量计算
            double diameter = componentIn.GJ_Diameter;
            double S = Math.Round(Math.Pow(diameter / 2, 2) * Math.PI, 2);
            double W = Math.Round(Math.Pow(diameter / 2, 2) * Math.PI * 7.85 / 1000, 2);
            componentOut.GJ_Area = S;
            componentOut.GJ_Weight = W;

            //钢管重量计算
            componentOut.GG_Weight = CalPipe(componentIn.GG_Diameter,componentIn.GG_Thickness);

            //钢板重量计算
            double plateThickness = componentIn.GB_Thickness;
            componentOut.GB_Weight = Math.Round(plateThickness * 7.85, 2);

            //金属网重量计算
            double Diameter = componentIn.JSW_Diameter;
            double interval = componentIn.JSW_Spacing;
            double n = Math.Round(1000 / interval + 2, 0);
            double w = Math.Round(n * 2 * 1.1 * Math.Pow(Diameter, 2) * Math.PI / 4 * 7.85 / 1000, 2);
            componentOut.JSW_Radical = n;
            componentOut.JSW_Weight = w;

            //锚杆托板重量
            double Thickness = componentIn.TB_Thickness;
            double L = componentIn.TB_Side;
            double Board_W = Math.Round(Thickness * L * L * 7.85 / 1000000, 2);
            componentOut.TB_Weight = Board_W;
            //锚杆重量
            double MG_diameter = componentIn.MG_Diameter;
            double BoltL = componentIn.MG_Length;
            double Bolt_W = Math.Round((BoltL * (MG_diameter * MG_diameter * Math.PI / 4 * 7.85 / 1000000)), 2);
            componentOut.MG_Weight = Bolt_W;

            componentOut.YTMG_Weight = Board_W + Bolt_W + 0.15;

            //吊钩重量
            double d = componentIn.DG_Diameter;
            double L_dg = componentIn.DG_Length;
            double R = d * 4;
            double Length = Math.Round(2 * Math.PI * R + L_dg - R, 0);
            double w_dg = Math.Round(Length * d * d * Math.PI / 4 * 7.85 / 1000000, 2);
            componentOut.DG_WGR = R;
            componentOut.DG_TotalLength = Length;
            componentOut.DG_Weight = w_dg;

            //管子挂钩重量计算
            double d_gz = componentIn.GZ_Diameter;
            double H_gz = componentIn.GZ_Height;
            double R_gz = componentIn.GZ_R;
            double r_gz = 3 * d_gz / 2;
            double Length_gz = Math.Round((2 * R_gz + 6 + d_gz) * Math.PI / 2 + r_gz * Math.PI + (H_gz - r_gz - (2 * R_gz + 6 + d_gz) / 2) + 80, 0);
            double w_gz = Math.Round(Length_gz * d_gz * d_gz * Math.PI / 4 * 7.85 / 1000000, 2);
            componentOut.GZ_WGR = r_gz;
            componentOut.GZ_TotalLength = Length_gz;
            componentOut.GZ_Weight = w_gz;

            //角钢托板计算
            string size = componentIn.JG_Standard;
            double Length_jg = componentIn.JG_Length;
            double W_m = 0;
            switch (size)
            {
                case "L100×14":
                    W_m = 20.6;
                    break;
                case "L110×14":
                    W_m = 22.8;
                    break;
                case "L125×14":
                    W_m = 26.2;
                    break;
                case "L140×14":
                    W_m = 29.5;
                    break;
                case "L140×16":
                    W_m = 33.4;
                    break;
                case "L160×14":
                    W_m = 34;
                    break;
                case "L160×16":
                    W_m = 38.5;
                    break;
                default:
                    W_m = 0;
                    break;
            }
            double W_jg = Math.Round(W_m * Length_jg / 1000, 2);
            double Add = 0;
            if (Length_jg > 300)
            {
                Add = Math.Round(100 * 100 * 78.5 * 0.8 / 1000000, 2);
            }
            double W_ALL = W_jg + Add;
            componentOut.JG_PieceWeight = W_m;
            componentOut.JG_Weight = W_jg;
            componentOut.JG_Rib = Add;
            componentOut.JG_RibWeight = W_ALL;

            return componentOut;
        }
        /// <summary>
        /// 钢管重量计算
        /// </summary>
        /// <param name="OutDiameter">钢管外直</param>
        /// <param name="WallThickness">钢管壁厚</param>
        /// <returns></returns>
        private static double CalPipe(double OutDiameter, double WallThickness)
        {
            double result = 0;
            result = Math.Round(((Math.Pow(OutDiameter, 2) - Math.Pow((OutDiameter - (2 * WallThickness)), 2)) * Math.PI / 4 * 7.85 / 1000), 2);
            return result;
        }
    }
}
